import 'package:casdon/common/model/tuya_category.dart';
import 'package:casdon/modules/device/data/api/ciot_product_category_api.dart';
import 'package:casdon/modules/device/data/api/ciot_product_model_api.dart';

class ProductCategoryModel {
  final String? ciotId;
  final String? tuyaId;
  final String name;
  final List<ProductModelModel> modelList;

  ProductCategoryModel({
    required this.ciotId,
    required this.tuyaId,
    required this.name,
    required this.modelList,
  });

  ProductCategoryModel copyWithTuyaId(String tuyaId) {
    return ProductCategoryModel(
      ciotId: ciotId,
      tuyaId: tuyaId,
      name: name,
      modelList: modelList,
    );
  }

  ProductCategoryModel copyWithCiotId(String ciotId) {
    return ProductCategoryModel(
      ciotId: ciotId,
      tuyaId: tuyaId,
      name: name,
      modelList: modelList,
    );
  }

  void insertTuyaModel(List<TuyaLevel2Category> models) {
    final list = models
        .map((model) => ProductModelModel.fromTuya(model))
        .toList();
    modelList.insertAll(0, list);
  }

  void insertCiotModel(List<ResponseCiotProductModelApi> models) {
    final list = models
        .map((model) => ProductModelModel.fromCiot(model))
        .toList();
    modelList.insertAll(0, list);
  }

  factory ProductCategoryModel.fromTuya(TuyaLevel1Category model) {
    return ProductCategoryModel(
      tuyaId: model.level1Code,
      ciotId: null,
      name: model.name,
      modelList: [],
    );
  }

  factory ProductCategoryModel.fromCiot(ResponseCiotProductCategoryApi model) {
    return ProductCategoryModel(
      tuyaId: null,
      ciotId: model.id,
      name: model.name,
      modelList: [],
    );
  }
}

enum ProductSource { tuya, ciot }

class ProductModelModel {
  final String icoUrl;
  final String name;
  final String id;
  final ProductSource source;

  ProductModelModel({
    required this.icoUrl,
    required this.name,
    required this.id,
    required this.source,
  });

  factory ProductModelModel.fromTuya(TuyaLevel2Category model) {
    return ProductModelModel(
      icoUrl: model.icon,
      name: model.name,
      id: model.pid,
      source: ProductSource.tuya,
    );
  }

  factory ProductModelModel.fromCiot(ResponseCiotProductModelApi model) {
    return ProductModelModel(
      icoUrl: model.imgUrlStr,
      name: model.name,
      id: model.pid,
      source: ProductSource.ciot,
    );
  }
}
